//1
package TUI;

import Ctr.CustomerCtr;
import Model.Customer;
import java.util.Scanner;

/**
 *
 */
public class CustomerUI {

    private CustomerCtr customerCtr;
    private InputUI inputUI;

    /**
     *
     */
    public CustomerUI() {
        customerCtr = new CustomerCtr();
        inputUI = new InputUI();
    }

    /**
     * Start the Menu
     */
    public void start() {
        customerMenu();
    }

    /**
     *
     */
    public void customerMenu() {
        try {
            boolean exit = false;
            while (!exit) {
                //show menu and return the user's choice
                int choice = writeCustomerMenu();
                switch (choice) {
                    case 1:
                    createCustomer();
                    break;
                    case 2:
                    deleteCustomer();
                    break;
                    case 3:
                    startUpdateCustomerUI();
                    break;
                    case 4:
                    int customerID = inputUI.inputCustomerID();
                    String type = inputUI.inputType();
                    int regNr = inputUI.inputRegNr();
                    int cardNr = inputUI.inputCardNr();
                    assignAccount(customerID, type, regNr, cardNr);
                    break;
                    case 5:
                    getCustomer();
                    break;
                    case 6:
                    printListOfCustomer();
                    break;
                    case 0:
                    exit = true;
                    break;

                }
            }
        } catch (Exception e) {
            start();
        }
    }

    /**
     *
     * @return
     */
    public int writeCustomerMenu() {  
        Scanner keyboard = new Scanner(System.in);

        System.out.println ("\f *** Customer menu ***");
        System.out.println ("(0) Return");
        System.out.println ("(1) Create Customer");
        System.out.println ("(2) Delete customer");
        System.out.println ("(3) Update the client");
        System.out.println ("(4) Assign bank account");
        System.out.println ("(5) Find Customer");
        System.out.println ("(6) Print list of customers");
        System.out.print ("\n Enter choice:");

        int choise = keyboard.nextInt();
        return choise;
    }

    /**
     * Delete a customer from the system using his CustomerID
     */

    private void deleteCustomer() {
        int customerID = inputUI.inputID();
        if (customerCtr.getCustomer(customerID) == null) {
            System.out.println ("the ID" + customerID + "does not exist. Please enter a new ID.");
        } else {
            boolean yesOrNo = inputUI.yesOrNo();
            if (yesOrNo == true) {
                customerCtr.deleteCustomer(customerID);
                System.out.println ("Customer with the ID" + customerID + "was deleted.");
            }
        }
    }

    /**
     * Create an account and assign the account to a customer
     * @param customerID
     * @param type
     * @param regNr
     * @param cardNr
     */
    public void assignAccount(int customerID, String type, int regNr, int cardNr) {
        customerCtr.giveAccount(customerID, type, regNr, cardNr);
        System.out.println ("Customer with id" + customerID + "has been added to the account.");
        inputUI.pause();
    }

    /**
     * Print all customers
     */
    public void printListOfCustomer() {
        for (Customer c : customerCtr.getCustomerList()) {

            System.out.println("ID: " + c.getCustomerID() + " Name: "
                + c.getName() + " Address: " + c.getAddress()
                + " Telephone: " + c.getPhone());
        }
        inputUI.pause();
    }

    /**
     * Find a customer looking for his ID
     */

    private void getCustomer() {
        int customerID = inputUI.inputID();
        Customer c = customerCtr.getCustomer(customerID);
        if (c != null) {
            System.out.println("ID: " + c.getCustomerID());
            System.out.println("Name: " + c.getName());
            System.out.println("Address: " + c.getAddress());
            System.out.println("Telephone: " + c.getPhone());
        } else {
            System.out.println ("The ID" + customerID + "was not found");
        }
        inputUI.pause();
    }

    /**
     * Creates a new Customer
     */
    private void createCustomer() {
        String name = inputUI.inputName();
        String address = inputUI.inputAddress();
        String phone = inputUI.inputPhone();
        int id = customerCtr.createCustomer(name, address, phone);
        System.out.println ("\n New customer created with id:" + id);
        inputUI.pause();
    }

    public void startUpdateCustomerUI() {
        int ID = inputUI.inputID();
        System.out.println("Customer found");
        System.out.println("\n" + customerCtr.getCustomer(ID));
        System.out.println("\nWrite new information");
        customerCtr.updateCustomer(inputUI.inputID(), inputUI.inputName(), inputUI.inputAddress(), inputUI.inputPhone());
        System.out.println("Customer successfully updated");
        inputUI.pause();
    }

}
